package cn.HuanG.base.controller;

import cn.HuanG.util.AjaxResult;
import cn.HuanG.util.FastDfsUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Controller
@RequestMapping("/fastDfs")
public class FastDfsController {
    @RequestMapping("/upload")
    @ResponseBody//返回路径所以用json
    public AjaxResult hello(MultipartFile file){
        //获取路径，动态获取后缀
        String filename = file.getOriginalFilename();
        String substring = filename.substring(filename.lastIndexOf(".") + 1);
        try {
            String path = FastDfsUtil.upload(file.getBytes(), substring);
            System.out.println(path);
            System.out.println(AjaxResult.me().setResultObj(path));
            return AjaxResult.me().setResultObj(path);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }
    @DeleteMapping
    @ResponseBody//返回路径所以用json
    public AjaxResult del(@RequestParam(required = true,value = "path") String path){
        String pathTmp = path.substring(1); // goup1/xxxxx/yyyy
        String groupName =  pathTmp.substring(0, pathTmp.indexOf("/")); //group1
        String remotePath = pathTmp.substring(pathTmp.indexOf("/")+1);// /xxxxx/yyyy
        System.out.println(groupName);
        System.out.println(remotePath);
        FastDfsUtil.delete(groupName, remotePath);
        return  AjaxResult.me();
    }
}
